//
//  LDSHeadStyleView.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/10/30.
//  Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSHeadStyleView.h"
@interface LDSHeadStyleView ()
@property(nonatomic, strong) UIImageView*bgImageView;
@end

@implementation LDSHeadStyleView

- (instancetype)init {
    if (self = [super init]) {
        [self viewSetup];
        [self frameSetup];
    }
    return self;
}

- (instancetype) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self viewSetup];
        [self frameSetup];
    }
    return self;
}

- (void)frameSetup {
    _bgImageView.frame = self.bounds;
    NSArray * frameArray = @[NSStringFromCGRect(CGRectMake(64,215 - 43 - 43, 43,43)),
                             NSStringFromCGRect(CGRectMake(self.frame.size.width/2 - 35,215 - 70 - 15, 70, 70)),
                             NSStringFromCGRect(CGRectMake(self.frame.size.width - 64 - 43,215 - 43- 43, 43,43))];
    for (NSInteger i = 0; i<_bgImageView.subviews.count; i++) {
        UIButton *sender = _bgImageView.subviews[i];
        sender.frame = CGRectFromString(frameArray[i]);
    }
    
}

- (void)viewSetup {
    _bgImageView = [[UIImageView alloc] init];
    _bgImageView.image = [UIImage imageNamed:@"home_img_bg_top"];
    _bgImageView.userInteractionEnabled = YES;
    _bgImageView.contentMode = UIViewContentModeScaleAspectFill;

    UIButton *subButtonOne = [UIButton buttonWithType:UIButtonTypeCustom];
    [subButtonOne addTarget:self action:@selector(subButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [subButtonOne setImage:[UIImage imageNamed:@"sercurity_icon_off_default"] forState:UIControlStateNormal];
    
    UIButton *subButtonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
    [subButtonTwo addTarget:self action:@selector(subButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [subButtonTwo setImage:[UIImage imageNamed:@"sercurity_icon_stay_default"] forState:UIControlStateNormal];
    
    UIButton *subButtonThree = [UIButton buttonWithType:UIButtonTypeCustom];
    [subButtonThree addTarget:self action:@selector(subButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [subButtonThree setImage:[UIImage imageNamed:@"sercurity_icon_away_default"] forState:UIControlStateNormal];
    
    [self addSubview:_bgImageView];
    
    [_bgImageView addSubview:subButtonOne];
    [_bgImageView addSubview:subButtonTwo];
    [_bgImageView addSubview:subButtonThree];
}

- (void)subButtonClicked:(UIButton *)sender {
    UIButton *centerButton;
    for (UIButton *subBtn in _bgImageView.subviews) {
        if (subBtn.center.x == _bgImageView.center.x) {
            centerButton = subBtn;
            break;
        }
    }
    if (sender.center.x == _bgImageView.center.x) {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
        animation.fromValue = @(0);
        animation.toValue = @(2*M_PI);
        
        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.duration = 0.5;
        group.fillMode = kCAFillModeRemoved;
        group.removedOnCompletion = NO;
        group.animations = @[animation];
        [centerButton.layer addAnimation:group forKey:@"group"];
        return;
    }
    
    CGRect senderFrame = sender.frame;
    CGRect centerFrame = centerButton.frame;
    CGPoint senderCenter = sender.center;
    CGPoint centerCenter = centerButton.center;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    animation.fromValue = @(0);
    animation.toValue = @(2*M_PI);
    CABasicAnimation *postion = [CABasicAnimation animationWithKeyPath:@"position"];
    postion.fromValue = [NSValue valueWithCGPoint:centerCenter];
    postion.toValue = [NSValue valueWithCGPoint:senderCenter];
    CABasicAnimation *postion2 = [CABasicAnimation animationWithKeyPath:@"position"];
    postion2.fromValue = [NSValue valueWithCGPoint:senderCenter];
    postion2.toValue = [NSValue valueWithCGPoint:centerCenter];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.duration = 0.5;
    group.fillMode = kCAFillModeRemoved;
    group.removedOnCompletion = NO;
    group.animations = @[animation,postion];
    CAAnimationGroup *group2 = [CAAnimationGroup animation];
    group2.duration = 0.5;
    group2.fillMode = kCAFillModeRemoved;
    group2.removedOnCompletion = NO;
    group2.animations = @[animation,postion2];
    
    [centerButton.layer addAnimation:group forKey:@"group"];
    [sender.layer addAnimation:group2 forKey:@"group2"];
    centerButton.frame = senderFrame;
    sender.frame = centerFrame;
}



@end
